package kr.ac.hanyang.cs.park;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import kr.ac.hanyang.cs.park.DBConstants.Task;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;


// Task의 정보를 갖고 있는 Class
public class TaskInfo {
	private String _id									= "";
	private String taskName								= "";
	private String taskOption							= "";
	private String description							= "";
	private Drawable icon								= null;
	private TaskType taskType							= null;
	private boolean isDebug								= false;

	private TaskDBHelper taskDBHelper					= null;
	private SQLiteDatabase taskDB						= null;


	public TaskInfo() {
		
	}

	public TaskInfo(String taskName, TaskType taskType, Drawable icon, String taskOption) {
		this.taskName									= taskName;
		this.taskType									= taskType;
		this.icon										= icon;
		this.taskOption									= taskOption;
	}

	public void setDebug(boolean isDebug) {
		this.isDebug									= isDebug;
	}


	private void prettyPrint() {
		Log.d("TaskInfo", icon + " || " + taskName + " || " + taskType + " || " + taskOption + " || " + description + " || " + isDebug );
	}


	public ArrayList<TaskInfo> getTaskInfo(Context context) {
		ArrayList<TaskInfo> taskList					= getAllTaskList(context);

		// print tasks when debug mode
		if (isDebug) {
			final int taskCount							= taskList.size();
			Log.d("TaskInfo", "Registered Tasks:"); 
			Log.d("TaskInfo", "No of Tasks: "+ taskCount);
			for (int i = 0;i < taskCount;i++)
				taskList.get(i).prettyPrint();
		}


		return taskList;
	}

    
	private ArrayList<TaskInfo> getAllTaskList(Context context) {
		ArrayList<TaskInfo> taskList					= new ArrayList<TaskInfo>();
		TaskType[] TaskList								= TaskType.values();

		// get all registered tasks from somewhere
	    taskDBHelper									= new TaskDBHelper(context);
	    taskDB											= taskDBHelper.getReadableDatabase();

	    Cursor c										= taskDB.query(Task.TABLE_NAME, null, null, null, null, null, null);
	    c.moveToFirst();

	    while (!c.isAfterLast()) {
	    	String _id									= c.getString(0);
	    	String taskName								= c.getString(1);
	    	TaskType taskType							= CastSpell.getTaskType(c.getString(2));
	    	byte[] iconByte								= c.getBlob(3);
	    	String taskOption							= c.getString(4);
	    	String taskDescription						= c.getString(5);


	    	// convert taskIcon to Drawable
	    	ByteArrayInputStream iconStream				= new ByteArrayInputStream(iconByte);
	    	Bitmap iconBitmap							= BitmapFactory.decodeStream(iconStream);
			@SuppressWarnings("deprecation")
			Drawable taskIcon							= new BitmapDrawable(iconBitmap);



	    	// generate temporary task to add
	    	TaskInfo aTask								= new TaskInfo();
	    	aTask.setId(_id);
	    	aTask.setTaskName(taskName);
	    	aTask.setTaskType(taskType);
	    	aTask.setIcon(taskIcon);
	    	aTask.setTaskOption(taskOption);
	    	aTask.setDescription(taskDescription);

	    	taskList.add(aTask);
	    	
	    	c.moveToNext();
	    }

	    c.close();
	    taskDB.close();
	    taskDBHelper.close();


	    return taskList;
	}


	public String getTaskId() {
		return _id;
	}


	public String getTaskName() {
		return taskName;
	}


	public String getTaskOption() {
		return taskOption;
	}


	public String getDescription() {
		return description;
	}


	public Drawable getIcon() {
		return icon;
	}


	public TaskType getTaskType() {
		return taskType;
	}

	public String getId() {
		return _id;
	}

	
	public void setId(String _id) {
		this._id = _id;
	}

	public void setTaskName(String taskName) {
		this.taskName									= taskName;
	}


	public void setTaskOption(String taskOption) {
		this.taskOption									= taskOption;
	}


	public void setDescription(String description) {
		this.description								= description;
	}


	public void setIcon(Drawable icon) {
		this.icon										= icon;
	}


	public void setTaskType(TaskType taskType) {
		this.taskType									= taskType;
	}

	
}
